<?php



class UserService extends BaseService
{
    private static $self        = NULL;
    private $mUser              = NULL;
    private $mProject           = NULL;
    private $mBankManage        = NULL;
    private $mMessage           = NULL;
    private $mExperScale        = NULL;
    private $mUserExtend        = NULL;
    private $mInvitie           = NULL;
    private $mRechargeRecord    =  NULL;
    private $mDeposit           =  NULL;
    private $mLookWithdraw      =  NULL;
    private $mLinechart         =  NULL;
    private $mInvestRecord      =  NULL;
    private $mLog               =  NULL;



    public function __construct()
    {

        $this->mProject         = new Involved_ProjectModel();
        $this->mBankManage      = new Involved_BankManageModel();
        $this->mMessage         = new Involved_MessageModel();
        $this->mExperScale      = new Involved_ExperScaleModel();
        $this->mUserExtend      = new Involved_UserExtendModel();
        $this->mInvitie         = new Involved_InvitieModel();
        $this->mRechargeRecord  = new Involved_RechargeRecordModel();
        $this->mUser            = new Involved_UserModel();
        $this->mDeposit         = new Involved_DepositModel();
        $this->mLookWithdraw    = new Involved_LookWithdrawModel();
        $this->mLinechart       =   new Involved_LineChartModel();
        $this->mInvestRecord    =   new Involved_InvestRecordModel();
        $this->mLog             =   new Involved_LogModel();

    }

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }

    /**
     * 登录用户信息
     */
    public function GetUserInformation()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->GetUserInformation($user_id);
    }
    /**
     * 账户信息项目推荐
     * @return mixed
     */
    public function UserAccount()
    {
        return $this->mProject->UserAccount();
    }

    /**
     * 插入充值金额
     * @param $paramObj
     * @return int
     */
    public  function Extendmoney($paramObj){
        $UserE      =$this->mUserExtend->UserExtendone($paramObj->uid);//获取总金额
        $amount     =$UserE->user_balance;//账户总金额
        if(!empty($amount)){
            $amount     =$UserE->user_balance+$paramObj->amount;//账户总金额
        }else{
            $amount     =$paramObj->amount;
        }
        #接受信息
        $aData = [
            'user_balance'      =>$amount,
            'user_id'           =>$paramObj->uid
        ];
        return $this->mUserExtend->update($aData,['user_id' =>$paramObj->uid]);

    }

    /**
     * 折线图信息
     * @return mixed
     */
    public function LineChart()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mLinechart->LineChart($user_id);
    }

    public function UserAccountChart()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $year = date('Y',time());
        return $this->mLinechart->UserAccountChart($user_id,$year);
    }
    /**
     * 银行卡管理
     * @return mixed
     */
    public function UserBankcard()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mBankManage->UserBankcard($user_id);
    }

    /**
     * 银行卡管理(唯一一张)
     * @return mixed
     */
    public function GetBankManageOne()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mBankManage->GetBankManageOne($user_id);
    }

    /**
     * 判断银行卡是否已经绑定
     * @return mixed
     */
    public function GetBankManageyeno()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mBankManage->GetBankManageyeno($user_id);
    }

    public function UserBankcardUpdata($paramObj){
        #接受信息
        $aData = [
            'kaihuhang'     => $paramObj->kaihuhang,
            'bank_num'      => $paramObj->bank_num,
            'bank_name'     => $paramObj->bank_name,
            'bound_mobile'  => $paramObj->bound_mobile,
            'bound_name'    => $paramObj->bound_name,
            'province'      => $paramObj->province,
            'city'          => $paramObj->city,
            'area'          => $paramObj->area,
            'bank_code'     => $paramObj->bank_code,
            'user_id'       => session_get(UserEnum::USER_INFO_USER_ID),
            'created'       => time(),
            'status'        => 2,
        ];
        return $this->mBankManage->update($aData,['bank_id' => $paramObj->bank_id]);
    }
    /**
     * 银行卡绑定
     * @param $paramObj
     * @return int
     */
    public function UserBankcardBound($paramObj)
    {
        #接受信息
        $aData = [
            'kaihuhang'     => $paramObj->kaihuhang,
            'bank_num'      => $paramObj->bank_num,
            'bank_name'     => $paramObj->bank_name,
            'bound_mobile'  => $paramObj->bound_mobile,
            'bound_name'    => $paramObj->bound_name,
            'status'        => 2,
            'province'      => $paramObj->province,
            'city'          => $paramObj->city,
            'area'          => $paramObj->area,
            'bank_code'     => $paramObj->bank_code,
            'user_id'       => session_get(UserEnum::USER_INFO_USER_ID),
            'created'       => time(),
        ];

        return $this->mBankManage->insert($aData);
    }
    /**
     * 判断身份证是否已经验证
     * @return mixed
     */
    public function Certificate()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Certificate($user_id);
    }

    /**
     * 读取认证的姓名和身份证数据
     * @return mixed
     */
    public function Certificatename()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Certificatename($user_id);
    }

    /**
     * 插入银行卡信息
     * @param $paramObj
     * @return int
     */
    public function UserMoneyajax($paramObj)
    {
        #接受信息
        $aData = [
            'bank_name' => $paramObj->myselect,
            'bank_num' => $paramObj->yinhang,
            'bound_mobile' => $paramObj->phone,
            'user_id' => session_get(UserEnum::USER_INFO_USER_ID),
            'status' => 1,
            'created' => time(),
        ];

        return $this->mBankManage->insert($aData);
    }

    /**
     * 删除银行卡验证
     * @param $pay_pwd
     * @return mixed
     */
    public function CheckBankPay($pay_pwd)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);

        $data = $this->mUser->CheckPayPwd($user_id);
        $pay_pwd = md5(md5($pay_pwd).$data->pay_num);
        return $this->mUser->CheckBankPwd($pay_pwd);

    }
    public function UserBankcardUp($bank_id)
    {
        return $this->mBankManage->UserBankcardUp($bank_id);
    }
    /**
     * 银行卡解绑
     * @param $paramObj
     * @return int
     */
    public function UserBankcardUnbound($paramObj)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $userinform=$this->mUser->GetUserInformation($user_id);
        $pay_num=$userinform->pay_num;//随机数
        $pwd    =md5(md5($paramObj->pay_pwd).$pay_num);
        if($pwd==$userinform->pay_pwd){
            $array= array(
                'bank_id' =>$paramObj->bank_id,
                'user_id' =>$user_id,
            );
            $mankbank=$this->mBankManage->delete($array);
            if(!empty( $mankbank)){
                $mess['code']   = '1';
                $mess['mess']   = '解除银行卡成功';
                return $mess;
            }else{
                $mess['code']   = '2';
                $mess['mess']   = '解除银行卡失败';
                return $mess;

            }
        }else{
            $mess['code']   = '2';
            $mess['mess']   = '密码错误,请输入密码';
            return $mess;
        }



    }

    /**
     * 修改登录密码
     * @param $paramObj
     * @return bool
     * @throws Exception
     */
    public function UpLoginPwd($paramObj)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $now_pwd = $this->mUser->NowPwd(md5($paramObj->now_pwd), $user_id);

        if (!$now_pwd) {
            throw new Exception('密码有误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }

        if ($now_pwd->password == md5($paramObj->new_pwd)) {
            throw new Exception('不能和以前密码重复', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }
        #验证码
        if (strtolower($paramObj->captcha) != strtolower(Session::get('ypt_Captcha'))) {
            throw new Exception('验证码错误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }
        $aData['password'] = md5($paramObj->new_pwd);
        return $this->mUser->update($aData, ['user_id' => $user_id]);
    }

    public function CheckNowPwd($paramObj)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->NowPwd(md5($paramObj->now_pwd), $user_id);
    }

    /**
     * 修改支付密码
     * @param $paramObj
     * @return bool
     * @throws Exception
     */
    public function UpPayPwd($paramObj)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $news = session_get(UserEnum::CLIENT_NEWS_CAPTCHA);

        #短信验证码
        if ($paramObj->news_captcha != $news['news_captcha']) {
            throw new Exception('短信验证码有误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }

        #验证码
        if (strtolower($paramObj->pay_captcha) != strtolower(Session::get('ypt_Captcha'))) {
            throw new Exception('验证码错误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }

        #支付密码验证
        if (!preg_match('/^\d{6}$/', $paramObj->pay_new_pwd)) {
            throw new Exception('支付密码有误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }

        $pay_num = rand(1000, 9999);
        $aData = [
            'pay_num' => $pay_num,
            'pay_pwd' => md5(md5($paramObj->pay_new_pwd) . $pay_num),
        ];


        return $this->mUser->update($aData, ['user_id' => $user_id]);
    }

    /**
     * 投资总金额
     * @return mixed
     */
    public  function InvestTicketnum(){

        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}

        return $this->mUser->InvestTicketnum($user_id);
    }
    /**
     *到期收益入库
     *
     */
    public  function IncomeFlownum(){
        $user_id    =session_get(UserEnum::USER_INFO_USER_ID);
        $ticket     =$this->mUser->InvestTicketpende($user_id);
        //return $ticket;
        $expersum   = UserService::instance()->experiencemoneywei();//体验金是否为空
        foreach ($ticket as $k => $v) {
        $str='';
        $time   =$v->invest_time;//获得当前时间戳
        $time3 = strtotime(date('Y-m-d H:i:s', time()));//获得当前时间戳 
        $time2 = strtotime(date('Y-m-d H:i:s',$time+ $v->limit*24*3600));//到期时间       
         // $time3  =strtotime(date("Y-m-d H:i:s",time()));//获得当前时间戳
         // $time   =$v->invest_time;//获得当前时间戳
         // $time2  =strtotime(date("Y-m-d H:i:s",$time+60*30));//到期时间
        // return $time2;
        //return $time2;
        if ($time2 >= $time3) {          
                              
        }else{
            $sum=''; 
            $str = $v->invest_money*$v->limit*($v->yield_rate/100);
            $sum =$v->invest_money;              
            $countmoney=$this->mUser->UserAlgorithm($str,$expersum,$sum);//总收益
            $Extendone=$this->mUserExtend->UserExtendone($user_id);
            $asdmoney=$Extendone->user_balance+$countmoney; 
            //return $countmoney;
            $dataarray=array('user_balance' =>$asdmoney);      
            $extends=$this->mUserExtend->update($dataarray,['user_ext_id' =>$user_id]);
            $aData = array(
                'status' => 1,
            );
            $this->mInvestRecord ->update($aData, ['invest_time' => $v->invest_time]);           
            // return $extends;        
        }
    }

    } 
    /**
     * 投资已经收益总金额
     * @return mixed
     */
    public  function InvestTicketgains(){
        $year=date("Y");//判断年份
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        $ticket=$this->mUser->InvestTicketgains($user_id);
        $str='';
        foreach ($ticket as $k => $v) {
            $str += $v->invest_money*$v->limit*($v->yield_rate/100);
        }
        if(($year%4==0)&&($year%100!=0)||($year%400==0)){
            $gains=$str/366;
            return $gains;
        }
        else{
            $gains=$str/365;
            return  $gains;
        }
        // return $year;
    }
    /**
     * 投资收益中总金额
     * @return mixed
     */
    public  function InvestTicketpende(){
        $year=date("Y");//判断年份
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        $ticket=$this->mUser->InvestTicketpende($user_id);
        $str='';
        foreach ($ticket as $k => $v) {
            $str += $v->invest_money*$v->limit*($v->yield_rate/100);
        }
        if(($year%4==0)&&($year%100!=0)||($year%400==0)){
            $gains=$str/366;
            return $gains;
        }
        else{
            $gains=$str/365;
            return  $gains;
        }
        // return $year;
    }

    /**
     * 账户信息正在收益中金额
     * @return mixed
     */
    public function UserAccountpende(){
        $year=date("Y");//判断年份
        $user_id    = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        $ticket     =$this->mUser->InvestTicketpende($user_id);
        $expersum   = UserService::instance()->experiencemoneywei();//体验金是否为空
        //return $ticket;
        if(!empty($ticket)){
            $str='';
            foreach ($ticket as $k => $v) {
                $str += $v->invest_money*$v->limit*($v->yield_rate/100);
            }
            if(($year%4==0)&&($year%100!=0)||($year%400==0)){
                $gains=($str/366)+$expersum;
                return $gains;
            }
            else{
                $gains=($str/365)+$expersum;
                return  $gains;
            }

        }else{
            $gains=$expersum;
            return  $gains;
        }

    }

    /**
    *冻结资金
    *
    */
    public function FrozenMoney(){
        $year=date("Y");//判断年份
        $user_id    = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        $ticket     =$this->mUser->InvestTicketpende($user_id);
        $str='';
        foreach ($ticket as $k => $v) {
            $str += $v->invest_money;
        }
        return  $str;
        if(($year%4==0)&&($year%100!=0)||($year%400==0)){
            $gains=($str/366)+$expersum;
            return $gains;
        }
        else{
            $gains=($str/365)+$expersum;
            return  $gains;
        }

    }



     /**
     * 账户信息收益完成金额
     * @return mixed
     */
    public function UserAccountgains(){
        $year=date("Y");//判断年份
        $user_id    = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        $ticket     =$this->mUser->InvestTicketgains($user_id);
        $expersum   = UserService::instance()->experiencemoney();//体验金是否为空
        if(!empty($expersum)){
            $expersum;
        }else{
            $expersum=0;
        }
       // print_r($expersum);
        $str='';
        foreach ($ticket as $k => $v) {
            $str += $v->invest_money*$v->limit*($v->yield_rate/100);
        }
        if(($year%4==0)&&($year%100!=0)||($year%400==0)){
            $gains=($str/366)+$expersum;
            return $gains;
        }
        else{
            $gains=($str/365)+$expersum;
            return  $gains;
        }
    }

    /**
     * 投资记录
     * @return mixed
     */
    public function UserInvestTicket($paramObj)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (is_object($paramObj)) {
            $page = $paramObj->page;
        } else {
            $page = $paramObj;
        }
        $year = '';
        $month = '';
        $p_type = '';
        if(!empty($paramObj->year1)&&!empty($paramObj->p_type)){
            $year = $paramObj->year1;
            $month = $paramObj->month1;
            $p_type = $paramObj->p_type;
            if(!empty($paramObj->month1)){
                $first = mktime(0, 0, 0, date("$paramObj->month1"), 1, date("$paramObj->year1"));
                $end = mktime(23, 59, 59, date("$paramObj->month1"), date("t"), date("$paramObj->year1"));
            }else{
                $first = mktime(0, 0, 0, date("1"), 1, date("$paramObj->year1"));
                $end = mktime(23, 59, 59, date("12"), date("t"), date("$paramObj->year1"));
            }
            $data = $this->mUser->SelDate($user_id, $first, $end, $paramObj->p_type, $paramObj->page);
            $count = $this->mUser->SelDatePage($user_id, $first, $end, $paramObj->p_type);
        }elseif(!empty($paramObj->p_type)){
            $p_type = $paramObj->p_type;
            $data = $this->mUser->SelDate2($user_id,$paramObj->p_type,$paramObj->page);
            $count = $this->mUser->SelDatePage2($user_id,$paramObj->p_type);
        }else{
            $data = $this->mUser->UserInvestTicketPage($user_id, $page);
            $count = $this->mUser->UserInvestTicket($user_id);
        }

        //总条数
        $total = count($count);
        //每页显示条数
        $page_size = 10;
        //总页数
        $page_num = ceil($total / $page_size);
        //获取当前页
        //$page=isset($_GET['page'])?intval($_GET['page']):1;
        //上一页
        $last_page = $page - 1 > 1 ? $page - 1 : 1;
        //下一页
        $next_page = $page + 1 < $page_num ? $page + 1 : $page_num;
        $str = '';
        $val = '';
        $ajaxpage = '';

        foreach ($data as $v) {
            $val .= "<tbody><tr>";
            $val .= "<td>$v->project_name</td>";
            $val .= "<td>$v->invest_money</td>";
            $val .= "<td>" . date('Y-m-d H:i', $v->invest_time) . "</td>";
            $val .= "<td>$v->yield_rate</td>";
            $val .= "<td>$v->limit</td>";
            $val .= "<td>$v->yield_money</td>";  
            if($v->status==2){
                $val .= "<td>收益中</td>";
            }
            if($v->status==1){
                $val .= "<td>收益完成</td>";
            }
            $val .= "</tr></tbody>";
        }

        if(!empty($data)) {
            $ajaxpage .= "<ul class='pagination'>";
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page' data-page=1  data-month=$month data-year=$year data-p_type=$p_type>&#60</a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if ($page == $i) {
                    $ajaxpage .= "<li class='active'><a href='javascript:void(0);' class='page'  data-page=$i data-month=$month data-year=$year data-p_type=$p_type>$i</a></li>";
                } else {
                    $ajaxpage .= "<li><a href='javascript:void(0);' class='page'  data-page=$i data-month=$month data-year=$year data-p_type=$p_type>$i</a></li>";
                }
            }
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page' data-page=$page_num data-month=$month data-year=$year data-p_type=$p_type>&#62</a></li>";
            $ajaxpage .= "</ul>";
        }
            $str .= " <table><thead><tr><td>投资项目</td> <td>投资金额（元）</td><td>投资时间</td><td>年收益率</td><td>期限</td><td>收益金额（元）</td><td>项目状态</td></tr></thead>$val$ajaxpage</table>";



        return $str;

    }

    /**
     * 投资记录查询
     * @return mixed
     */
//    public function SelDate()
//    {
//
//        $first = mktime(0, 0 , 0,date("$paramObj->month1"),1,date("$paramObj->year1"));
//        $end = mktime(23,59,59,date("$paramObj->month1"),date("t"),date("$paramObj->year1"));
//
//        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
//        return $this->mUser->SelDate($user_id,$first,$end,$paramObj->p_type);
//    }
    /**
     * 报票记录
     * @return mixed
     */
    public function UserBTicket()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->UserBTicket($user_id);
    }

    /**
     * 账户明细
     * @return mixed
     */
    public function Account()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Account($user_id);

    }

    /**
     * 充值
     * @return mixed
     */
    public function Recharge()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Recharge($user_id);
    }

    /**
     * 插入充值
     * @param $paramObj
     * @return int
     */
    public function Rechargeinsert($paramObj)
    {
        #接受信息
        $aData = [
            'orderid'           =>$paramObj->id,
            'rechar_money'      =>$paramObj->amount,
            'oper_time'         =>$paramObj->time,
            'status'            =>1,
            'user_id'           =>$paramObj->uid,
            'type'              =>1
        ];
        return $this->mRechargeRecord->insert($aData);
       // return  $aData;
    }

    /**
     * 网关插入充值
     * @param $paramObj
     * @return int
     */
    public function Wgecharge($paramObj)
    {
        #接受信息
        $aData = [
            'orderid'           =>$paramObj->v_oid,
            'rechar_money'      =>$paramObj->v_amount,
            'oper_time'         =>time(),
            'status'            =>1,
            'type'              =>2,
            'bank'              =>$paramObj->v_pmode,
            'user_id'           =>$paramObj->uid,
        ];

        return $this->mRechargeRecord->insert($aData);
    }
    /**
     * 提现
     * @return mixed
     */
    public function WithDraw()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->WithDraw($user_id);
    }

    /**
     * 提现密码
     * @return mixed
     */
    public function AjaxWithdraw($paramObj)
    {
        $asda=session_get(UserEnum::USER_INFO_SESSION_KEY);
        if (!empty($asda->user_id )) {
            $pay_num=$asda->pay_num;//随机数
            $pwd    =md5(md5($paramObj->pay_pwd).$pay_num);
            $LookUsers  =$this->mLookWithdraw->lockwithdraw($asda->user_id);//获取人是不是本冻结
            if(!empty($LookUsers)){//如果被冻结，看是否到解锁时间
                $LookUsersa  =$this->mLookWithdraw->lockwithdrawone($asda->user_id);//获取人是不是本冻结
                //return  $LookUsersa;
                if(time()<$LookUsersa->locktime){
                    return('{"success":false,"msg":"对不起，您的账户已被锁定,请于'.date('Y-m-d H:i:s',$LookUsersa->locktime).'后尝试登录！"}');
                }else{
                    $LookUseraaaa=$this->mLookWithdraw->delete(['user_id'=>$asda->user_id]);
                }
            }
            if ($pwd==$asda->pay_pwd){
                $rdata=array(
                    'bank_num'      =>$paramObj->bank_num,
                    'name'          =>$paramObj->bound_name,
                    'moneynum'      =>$paramObj->moneysum,
                    //'user_id'       =>$asda->user_id,
                    'time'          =>time()
                );
                // print_r($rdata);
                $dataRecord= $this->mDeposit->insert($rdata);
                if (!empty($dataRecord)) {return('{"success":true,"msg":"提现成功!."}');}
            }else{
                $LookUsersa  =$this->mLookWithdraw->lockwithdrawone($asda->user_id);//获取本人有没有输入
               // print_r($LookUsersa->number);
                if(!empty($LookUsersa->number)){
                    // $tmpArr = explode('_',$LookUsersa->number);
                    $count = $LookUsersa->number;
                        if($count<= 4){
                            $count++;
                            $logins=$count;
                            $array=array(
                                'number'    =>$logins,
                                'user_id'   =>$asda->user_id,
                                'locktime'  =>time(),
                                'ip'        =>$_SERVER['REMOTE_ADDR'], 
                            );
                            if ($count<1) {
                                $LookUserd= $this->mLookWithdraw->insert($array);
                            }
                              if ($count>0&&$count<=4) {
                                $LookUserd= $this->mLookWithdraw->update($array,['id' => $LookUsersa->id]);
                            }

                        }
                        if($count == 5){
                            $data4['number'] = $count;
                            $data4['user_id'] = $asda->user_id;
                            $data4['locktime'] = time()+60*1;
                            $data4['ip'] = $_SERVER['REMOTE_ADDR'];
                            $LookUserd= $this->mLookWithdraw->update($data4,['id' => $LookUsersa->id]);
                            if (!empty($LookUserd)) {
                                return('{"success":false,"msg":"您已经连续5次输入错误密码，账户被锁定30分钟"}');
                            }
                        }
                        return('{"success":false,"msg":"密码错误,这是你第'.$count.'次,一共5次机会"}');
                }else{
                    // cookie('logins', $user . '_1');
                     $logins=1;
                     $array=array(
                                'number'    =>$logins,
                                'user_id'   => $asda->user_id,
                                'locktime'  =>time(),
                                'ip'        =>$_SERVER['REMOTE_ADDR'], 
                            );
                $LookUserd= $this->mLookWithdraw->insert($array);
                }
                if (empty($LookUsersa->number)) {
                   return('{"success":false,"msg":"密码错误,这是你第1次,一共5次机会"}');
                }
               
            }
        }else{
            return('{"success":false,"msg":"你还没有登录,请登录！！！"}');
        }

    }
    /**
     * 实名认证
     * @param $paramObj
     * @return bool|int
     * @throws Exception
     */
    public function RealNameAuth($paramObj)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);

        #参数验证
        if (empty($paramObj->real_name) || empty($paramObj->id_card)) {
            throw new Exception('必填项不能为空', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        #身份证验证
        if (!preg_match('/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/', $paramObj->id_card)) {
            throw new Exception("身份证有误");
        }

        #身份证是否已经被绑定
        $id_card = $this->mUser->CheckCard($paramObj->id_card);

        if(!$id_card){
            $is_card = $this->mUser->CheckIsCard($user_id);

            if(empty($is_card->id_card)){
                 return 1;
//                #接受信息
//                    $aData = [
//                        'real_name' => $paramObj->real_name,
//                        'id_card' => $paramObj->id_card,
//                    ];
//
//                    return $this->mUser->update($aData,['user_id'=>$user_id]);
            }else{
                return 2;
            }
        }else{
            return 3;
        }
//        if (!$id_card) {
//            $user_id = session_get(UserEnum::USER_INFO_USER_ID);
//            $is_card = $this->mUser->CheckIsCard($user_id);
//            if (!$is_card) {
//                $url = "http://api.avatardata.cn/IdCardCertificate/VerifyAndInfo?key=42b49891a9ff402db67a91a967d3b2bd&realname=$paramObj->real_name&idcard=$paramObj->id_card&format=true";
//                $ch = curl_init();
//                curl_setopt($ch, CURLOPT_URL, $url);
//                curl_setopt($ch, CURLOPT_HEADER, 0);
//                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//                curl_setopt($ch, CURLOPT_TIMEOUT, 150);
//                $json = curl_exec($ch);
//                curl_close($ch);
//                $result = json_decode($json, true);
//                if ($result['error_code'] == 0) {
//                    #接受信息
//                    $aData = [
//                        'real_name' => $paramObj->real_name,
//                        'id_card' => $paramObj->id_card,
//                    ];
//
//                    return $this->mUser->update($aData,['user_id'=>$user_id]);
//                }
//            } else {
//                return 1;
//            }
//        } else {
//            return 2;
//        }

    }

    public function UsernameAuthThr($paramObj)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if(empty($paramObj->real_name)&&empty($paramObj->id_card)){
            echo "<script>alert('验证失败,请重新验证');location.href='/username_auth_one'</script>";
        }
        $pay_num = rand(1000, 9999);
        $aData = [
            'pay_num'   => $pay_num,
            'pay_pwd'   => md5(md5($paramObj->pay_pwd) . $pay_num),
            'real_name' => $paramObj->real_name,
            'id_card'   => $paramObj->id_card,
        ];
        return $this->mUser->update($aData,['user_id'=>$user_id]);
    }
    //邀请加密
    public function UserInvite()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (!empty($user_id)) {
            return $this->mUser->UserInvite($user_id);
        }
    }

    //邀请解密
    public function UserInvitess($id)
    {

        return $this->mUser->UserInvitess($id);
    }

    //邀请人列表
    public function Reward()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $names = $this->mInvitie->GetInvestOne($user_id);


        return $this->mUser->Reward($user_id);


    }

    //邀请人总金额
    public function Rewardsum()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Rewardsum($user_id);
    }

    //邀请总人数
    public function  Rewardcount()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->Rewardcount($user_id);
    }

    //现金劵
    public function voucher()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->voucher($user_id);
    }

    /**
     *现金劵是否过期修改statue状态
     */
    public function vouchersta()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $voucher = $this->mUser->voucher($user_id);
        foreach ($voucher as $key => $v) {
            $data = date('Y-m-d', $v->time);
            $time3 = strtotime(date('Y-m-d', time()));//获得当前时间戳 $v->datetime
            $time2 = strtotime(date('Y-m-d', strtotime("$data+ 30 day")));//到期时间
            if ($time2 >= $time3) {
                $aData = array(
                    'statue' => 0,
                );
                $this->mInvitie->update($aData, ['time' => $v->time]);
            } else {
                $aData = array(
                    'statue' => 1,
                );
                $this->mInvitie->update($aData, ['time' => $v->time]);
            }
        }
        return $voucher;
        //return $this->mUser->experiencesum($user_id);
    }
    /**
     *现金券总数
     */
    public function vouchersum()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->vouchersum($user_id);
    }

    /**
     *体验金
     */
    public function experience()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->experience($user_id);
    }
    /**
     *体验金有效信息
     */
    public function experiencenot()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        return $this->mUser->experiencenot($user_id);
    }

    /**
     *体验金有效修改statue状态
     */
    public function experiencesta()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $experience = $this->mUser->experience($user_id);
        foreach ($experience as $key => $v) {
            $data = date('Y-m-d', $v->time);
            $time3 = strtotime(date('Y-m-d', time()));//获得当前时间戳 $v->datetime
            $time2 = strtotime(date('Y-m-d', strtotime("$data+ 30 day")));//到期时间
            if ($time2 >= $time3) {
                if ($v->statue!=2) {
                   $aData = array(
                    'statue' => 0,
                );
                $this->mExperScale->update($aData, ['time' => $v->time]);
                }
               
            } else {
                 if ($v->statue!=2) {
                   $aData = array(
                    'statue' => 1,
                  );
                $this->mExperScale->update($aData, ['time' => $v->time]);
                }
               
            }
        }
        return $experience;
        //return $this->mUser->experiencesum($user_id);
    }

    /**
     *体验金总数
     */
    public function experiencesum()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->experiencesum($user_id);
    }

    /**
     *体验金收益完成金额
     */
    public function experiencemoney()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        $experiencemoney = $this->mUser->experiencemoney($user_id);
        //print_r($experiencemoney);
        if(!empty($experiencemoney)){


            $money  = $experiencemoney[0]->exper_money;//金额
            $nums   = $experiencemoney[0]->exper_nums;//期限
            $time   = $experiencemoney[0]->exper_time;//投资时间
            $annua  = $experiencemoney[0]->exper_annua / 100;//年利率
            if (empty($money)||empty($nums)||empty($time)||empty($annua)) {return "参数错误！！！";}
            $shouyi = $money * $nums * $annua;
            $time3  =strtotime(date("Y-m-d H:i:s",time()));//获得当前时间戳
            $time2  =strtotime(date("Y-m-d H:i:s",$time+60*60*24));//到期时间
            if ($time2 <= $time3){
                $year=date("Y");//判断年份
                if(($year%4==0)&&($year%100!=0)||($year%400==0)){
                    $gains=$shouyi/366;
                    return $gains;
                }else{
                    $gains=$shouyi/365;
                    return  $gains;
                }
            }
        }

    }


    public  function qweqweqw(){
    $user_id = session_get(UserEnum::USER_INFO_USER_ID);
    return  $this->mUser->experiencenot($user_id);
    }

    /**
     *体验金正在收益金额
     */
    public function experiencemoneywei()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if (empty($user_id)) {return "对不起你还没有登录";}
        $experiencemoney = $this->mUser->experiencemoney($user_id);
        if(!empty($experiencemoney)){
            $money  = $experiencemoney[0]->exper_money;//金额
            $nums   = $experiencemoney[0]->exper_nums;//期限
            $time   = $experiencemoney[0]->exper_time;//投资时间
            $annua  = $experiencemoney[0]->exper_annua / 100;//年利率
            if (empty($money)||empty($nums)||empty($time)||empty($annua)) {return "参数错误！！！";}
            $shouyi = $money * $nums * $annua;
            $time3  =strtotime(date("Y-m-d H:i:s",time()));//获得当前时间戳
            $time2  =strtotime(date("Y-m-d H:i:s",$time+60*60*24));//到期时间
            if ($time2 >= $time3){
                $year=date("Y");//判断年份
                if(($year%4==0)&&($year%100!=0)||($year%400==0)){
                    $gains=$shouyi/366;
                    return $gains;
                }else{
                    $gains=$shouyi/365;
                    return  $gains;
                }
            }

        }else{
            $gains=0;
            return  $gains;
        }

    }


    /**
     * 消息列表
     */

    /**
     *全部
     * @return mixed
     */
    public function ReadAllPage($page)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $count = $this->mUser->ReadAll($user_id);
        $data = $this->mUser->ReadAllPage($user_id,$page);
        //return $this->mUser->UnReadPage($user_id);
        //总条数
        $total = count($count);
        //每页显示条数
        $page_size = 10;
        //总页数
        $page_num = ceil($total / $page_size);
        $str = '';
        $val = '';
        $ajaxpage = '';

        foreach ($data as $v) {
            $val .= "<li class='unread' ro='hide'>";
            $val .= "<input type='checkbox' name='check1' value='$v->message_id'>";
            $val .= "<div class='news-title left'>$v->title</div>";
            $val .= "<div class='news-time right'>".date('Y-m-d H:i:s',$v->created)."</div>";
            $val .= "<p class='news-cont'>$v->content</p>";
            $val .= "</li>";
        }

        if(!empty($data)){
            $ajaxpage .= "<ul class='pagination'>";
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page previous' data-page=1 ><span></span></a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if($page==$i){
                    $ajaxpage .= "<li class='active'><a href='javascript:void(0);' class='page '  data-page=$i >$i</a></li>";
                }else{
                    $ajaxpage .= "<li><a href='javascript:void(0);' class='page '  data-page=$i >$i</a></li>";
                }
            }
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page next' data-page=$page_num ><span></span></a></li>";
            $ajaxpage .= "</ul>";
        }

        $str .= "$val$ajaxpage";
        return $str;
    }

    /**
     * 未读
     * @return mixed
     */
    public function UnReadPage($page)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $count = $this->mUser->UnRead($user_id);
        $data = $this->mUser->UnReadPage($user_id,$page);
        //return $this->mUser->UnReadPage($user_id);
        //总条数
        $total = count($count);
        //每页显示条数
        $page_size = 10;
        //总页数
        $page_num = ceil($total / $page_size);
        $str = '';
        $val = '';
        $ajaxpage = '';

        foreach ($data as $v) {
            $val .= "<li class='unread' ro='hide'>";
            $val .= "<input type='checkbox' name='check2' value='$v->message_id'>";
            $val .= "<input type='hidden'  value='$v->message_id'>";
            $val .= "<div class='news-title left' name='title' >$v->title</div>";
            $val .= "<div class='news-time right'>".date('Y-m-d H:i:s',$v->created)."</div>";
            $val .= "<p class='news-cont'>$v->content</p>";
            $val .= "</li>";
        }

        if(!empty($data)){
            $ajaxpage .= "<ul class='pagination'>";
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page previous' data-page=1 ><span></span></a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if($page==$i){
                    $ajaxpage .= "<li class='active'><a href='javascript:void(0);' class='page '  data-page=$i >$i</a></li>";
                }else{
                    $ajaxpage .= "<li><a href='javascript:void(0);' class='page '  data-page=$i >$i</a></li>";
                }
            }
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page next' data-page=$page_num ><span></span></a></li>";
            $ajaxpage .= "</ul>";
        }

        $str .= "$val$ajaxpage";
        return $str;
    }

    /**
     * 已读
     * @return mixed
     */
    public function ReadPage($page)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $count = $this->mUser->Read($user_id);
        $data = $this->mUser->ReadPage($user_id,$page);
        //return $this->mUser->UnReadPage($user_id);
        //总条数
        $total = count($count);
        //每页显示条数
        $page_size = 10;
        //总页数
        $page_num = ceil($total / $page_size);
        $str = '';
        $val = '';
        $ajaxpage = '';

        foreach ($data as $v) {
            $val .= "<li class='unread' ro='hide'>";
            $val .= "<input type='checkbox' name='check3' value='$v->message_id'>";
            $val .= "<div class='news-title left'>$v->title</div>";
            $val .= "<div class='news-time right'>".date('Y-m-d H:i:s',$v->created)."</div>";
            $val .= "<p class='news-cont'>$v->content</p>";
            $val .= "</li>";
        }

        if(!empty($data)){
            $ajaxpage .= "<ul class='pagination'>";
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page previous' data-page=1 ><span></span></a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if($page==$i){
                    $ajaxpage .= "<li class='active'><a href='javascript:void(0);' class='page '  data-page=$i >$i</a></li>";
                }else{
                    $ajaxpage .= "<li><a href='javascript:void(0);' class='page '  data-page=$i >$i</a></li>";
                }
            }
            $ajaxpage .= "<li><a href='javascript:void(0);' class='page next' data-page=$page_num ><span></span></a></li>";
            $ajaxpage .= "</ul>";
        }

        $str .= "$val$ajaxpage";
        return $str;
    }


    /**
     * 未读
     * @return mixed
     */
    public function UnRead()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->UnRead($user_id);
    }

    /**
     * 全部
     * @return mixed
     */
    public function ReadAll()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUser->ReadAll($user_id);
    }


    /**
     * 消息列表删除
     * @param $paramObj
     * @return mixed
     */
    public function UserNewsDel($paramObj)
    {
        return $this->mUser->UserNewsDel($paramObj->num);
    }

    /**
     * 未读修改
     * @param $m_id
     * @return bool
     */
    public function UserNewsUp($m_id)
    {
        $aData['status'] = 1;
        return $this->mMessage->update($aData, ['message_id' => $m_id]);
    }

 

    public function JsWithdrawPwd($pay_pwd)
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $info = $this->mUser->getUserSession($user_id);
        if( $info->pay_pwd != md5(md5($pay_pwd).$info->pay_num)){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     * 数据加密
     * time:2016.05.17
     */
    public function UserMoneyjiami($input){
        $size = mcrypt_get_block_size ( MCRYPT_3DES, 'ecb' );
        $input =$this->pkcs5_pad($input, $size);
        $key = str_pad ( 'AQWSEDRFTGYHUJIKOLPXCVBN', 24, '0' );
        $td = mcrypt_module_open ( MCRYPT_3DES, '', 'ecb', '' );
        $iv = @mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
        @mcrypt_generic_init ( $td, $key, $iv );
        $data = mcrypt_generic ( $td, $input );
        mcrypt_generic_deinit ( $td );
        mcrypt_module_close ( $td );
        $data = base64_encode ( $data );
        $data = str_replace('+','&',$data);
        return $data;

    }

    public   function pkcs5_pad($text, $blocksize) {
        $pad = $blocksize - (strlen ( $text ) % $blocksize);
        return $text . str_repeat ( chr ( $pad ), $pad );
    }
    /**
     * 数据解密
     * time:2016.05.17
     */
    public    function pkcs5_unpad($text) {
        $pad = ord ( $text {strlen ( $text ) - 1} );
        //return $pad;
        if ($pad > strlen ( $text )) {
            return false;
        }
        if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) {
            return false;
        }
        return substr ( $text, 0, - 1 * $pad );
    }
    public  function UserMoneyjiamis($encrypted){
        $encrypted = str_replace('&','+',$encrypted);
        $encrypted = base64_decode ( $encrypted );
        //return $encrypted;
        $key = str_pad ( 'AQWSEDRFTGYHUJIKOLPXCVBN', 24, '0' );
        $td = mcrypt_module_open ( MCRYPT_3DES, '', 'ecb', '' );
        $iv = @mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
        $ks = mcrypt_enc_get_key_size ( $td );
        @mcrypt_generic_init ( $td, $key, $iv );
        $decrypted = mdecrypt_generic ( $td, $encrypted );
        mcrypt_generic_deinit ( $td );
        mcrypt_module_close ( $td );
        $y=$this->pkcs5_unpad( $decrypted );
        return $y;
    }


     /**
     *可体现金额
     */
    public function UserExtendone(){
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mUserExtend->UserExtendone($user_id);
        
    }


    /**
     * 银行卡四要素
     * @param $paramObj
     * @param $v_id
     * @return array
     */
    public function BankAuth($paramObj,$v_id)
    {
        return $this->mLog->BankAuth($paramObj,$v_id);
    }

    /**
     * 重新获取验证码
     * @param $paramObj
     * @return array
     */
    public function GetBankCode($paramObj)
    {
        return $this->mLog->GetBankCode($paramObj);
    }

    /**
     * 获取银行卡信息
     * @param $user_id
     * @return mixed
     */
    public function GetBankInfo($user_id)
    {
        return $this->mBankManage->GetBankInfo($user_id);
    }
}
